package top.songxs.gmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import top.songxs.gmall.common.execption.GmallException;
import top.songxs.gmall.common.result.ResultCodeEnum;
import top.songxs.gmall.product.entity.BaseTrademark;
import top.songxs.gmall.product.entity.SkuInfo;
import top.songxs.gmall.product.entity.SpuInfo;
import top.songxs.gmall.product.service.BaseTrademarkService;
import top.songxs.gmall.product.mapper.BaseTrademarkMapper;
import org.springframework.stereotype.Service;
import top.songxs.gmall.product.service.SkuInfoService;
import top.songxs.gmall.product.service.SpuInfoService;

import java.util.List;

/**
 * @author 86176
 * @description 针对表【base_trademark(品牌表)】的数据库操作Service实现
 * @createDate 2023-05-15 20:14:07
 */
@Service
public class BaseTrademarkServiceImpl extends ServiceImpl<BaseTrademarkMapper, BaseTrademark>
        implements BaseTrademarkService {

    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    private SkuInfoService skuInfoService;

    @Override
    public Page<BaseTrademark> findBaseTrademarkByPage(Long page, Long limit) {
        Page<BaseTrademark> baseTrademarkPage = new Page<>(page, limit);
        this.page(baseTrademarkPage);
        return baseTrademarkPage;
    }

    @Override
    public void removeBaseTrademarkById(Long id) {
        //1:根据品牌Id查询spu的数据和sku数据

        //2:如果查询到数据了就说明当前要删除的品牌数据被spu所关联，不允许删除
        LambdaQueryWrapper<SpuInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SpuInfo::getTmId, id);
        List<SpuInfo> list = spuInfoService.list(wrapper);
        if (list != null && list.size() > 0) {
            throw new GmallException(ResultCodeEnum.REF_SPU_ERROR);
        }
        //3：根据品牌id查询sku数据，如果查询到了就说明当前删除的品牌数据被sku所关联，不允许删除
        LambdaQueryWrapper<SkuInfo> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(SkuInfo::getTmId, id);
        List<SkuInfo> list2 = skuInfoService.list(wrapper2);
        if (list2 != null && list.size() > 0) {
            throw new GmallException(ResultCodeEnum.REF_SKU_ERROR);
        }
        //4：如果都没查到则可以删除
        this.removeById(id);
    }
}













